package com.zekdot.password.dao;

import com.zekdot.password.entity.PasswordDataEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Create by zekdot on 19-8-6.
 */
@Mapper
@Repository
public interface PasswordDataDao {
    /**
     * 增加一条数据
     * @param passwordEntity 数据实体
     * @return
     */
    int insert(PasswordDataEntity passwordEntity);

    /**
     * 查询第一页的数据
     * @param length 长度
     * @return
     */
    @Select("SELECT * FROM password_data WHERE status=0 AND user_id=#{userId} ORDER BY id DESC LIMIT #{length}")
    List<PasswordDataEntity> selectFirst(@Param("userId") String userId,@Param("length") int length);

    /**
     * 查询更多页的数据
     * @param lastId 最后一条id
     * @param length 长度
     * @return
     */
    @Select("SELECT * FROM password_data WHERE user_id=#{userId} AND id<#{lastId} AND status=0 ORDER BY id DESC LIMIT #{length}")
    List<PasswordDataEntity> selectMore(@Param("userId")String userId,@Param("lastId") int lastId, @Param("length") int length);

    /**
     * 删除某条数据
     * @param id 数据id
     * @return
     */
    @Delete("UPDATE password_data SET status=1 WHERE id=#{id}")
    int delete(@Param("id") int id);

    /**
     * 根据关键字搜索密码信息
     * @param userId 用户id
     * @param keyword 关键字
     * @param lastId 最后一条id
     * @param length 长度
     * @return
     */
    @Select("SELECT * FROM password_data WHERE user_id=#{userId} AND keyword like #{keyword} AND id>#{lastId} AND status=0 ORDER BY ID ASC LIMIT #{length}")
    List<PasswordDataEntity> search(@Param("userId")String userId,@Param("keyword") String keyword, @Param("lastId") int lastId, @Param("length") int length);


    /**
     * 根据id获得某个具体的密码
     * @param id
     * @return
     */
    @Select("SELECT * FROM password_data WHERE id=#{id}")
    PasswordDataEntity get(@Param("id") int id);
}
